package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.jsonwebtoken.lang.Objects;
import org.apache.commons.codec.digest.DigestUtils;

import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Override
    public SystemUserItemVo getSystemUserItemVoById(Long id) {
        return baseMapper.selectSystemUserItemVoById(id);
    }

    @Override
    public IPage<SystemUserItemVo> pageSystemUserItemVo(Page<SystemUserItemVo> systemUserItemVoPage, SystemUserQueryVo queryVo) {
        return baseMapper.selectSystemUserItemVoListByQuery(systemUserItemVoPage,queryVo);
    }

    @Override
    public void saveOrUpdateMD5(SystemUser systemUser) {
        if (ObjectUtils.isEmpty(systemUser.getId())){
            // 添加状态  新增
            systemUser.setStatus(BaseStatus.ENABLE);
            String password = systemUser.getPassword();
            String md5Hex = DigestUtils.md5Hex(password);
            //密码加密
            systemUser.setPassword(md5Hex);
            // 设置默认url
            systemUser.setAvatarUrl("http://192.168.6.101:9000/lease/头像.jpg");
        }else {
            //更新时密码不为空,和空字符串,就加密更新密码
            if (ObjectUtils.isNotEmpty(systemUser.getPassword())){
                systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
            }
        }
        this.saveOrUpdate(systemUser);
    }
}




